package com.aaa.dao.impl;

import com.aaa.dao.StudentDao;
import com.aaa.util.BaseDao;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/23 11:23
 * @description：报名dao接口实现类
 * @modified By：
 * @version: 1.0
 */
public class StudentDaoImpl  extends BaseDao implements StudentDao {
    @Override
    public Map<String, Object> getBusinessInfo(Map map) {
        String sql="select  count(*) doneApply,bh.*  from student_hospital s right  join  business_hospital bh on s.business_id = bh.id where  bh.id=? group by s.business_id";
        Object[] params={map.get("businessId")};
        List<Map<String, Object>> mapList = executeQuery(sql, params);
        if(mapList!=null&&mapList.size()>0){
            return mapList.get(0);
        }
        return  null;
    }

    @Override
    public boolean addStudent(Map map) {
        String sql ="insert into student_hospital(name, age, phone, school, create_time, business_id) values(?,?,?,?,?,?)";
        Object[] params={map.get("name"),map.get("age"),map.get("phone"),map.get("school"),new Date(),map.get("business_id")};
        return executeUpdate(sql,params);
    }

    @Override
    public List<Map<String, Object>> findAllStudent(Map map) {
        String sql="select student_hospital.Id, student_hospital.name, age, phone, school, student_hospital.create_time, business_id,bh.name business_name from student_hospital left join business_hospital bh on student_hospital.business_id = bh.id";
        String makeSql = makeSql(sql, map);
        return super.executeQuery(makeSql,null);
    }

    @Override
    public int getCount(Map map) {
        StringBuffer stringBuffer = new StringBuffer(" select  count(*) myCount from student_hospital where 1=1 ");
        //拼接where条件  where username like  '%2%'
        Set set = map.keySet();
        for (Object o : set) {
            String  key=o.toString();
            if(key.contains("search")){
                //如果map的key中包含和关键字search，那么就是条件查询的条件
                stringBuffer.append(" and "+key.replace("search","")+" like "+"'%"+map.get(key)+"%'");
            }
        }
        List<Map<String, Object>> mapList = super.executeQuery(stringBuffer.toString(), null);
        if (mapList != null && mapList.size() > 0) {
            //获取第一行数据
            Map<String, Object> objectMap = mapList.get(0);
            //获取列名为myCount的列值
            Object count = objectMap.get("myCount");
            //将object类型的列值转换成int类型并返回
            return Integer.parseInt(count + "");
        }
        return 0;
    }
}
